package com.alibaba.citrus.cr.b2b.trade.order.functions.utils;

import com.alibaba.citrus.cr.b2b.trade.order.facade.common.SetOrderTag;
import com.alibaba.cz.base.tool.DateFormatUtil;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crb2btradecenter.dto.OrderDetailDTO;
import com.epoch.app.crb2btradecenter.dto.request.SaveSaleOrderRequest;
import com.epoch.app.crb2btradecenter.dto.response.OrderQueryResponse;
import com.epoch.app.crb2btradecenter.sdo.OrderLineSDO;
import com.epoch.app.crb2btradecenter.sdo.OrderSDO;
import com.epoch.app.crb2btradecenter.sdo.ScItemSDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
public class SaveOrderConverter {

    public static OrderSDO converterSDO(SaveSaleOrderRequest request) {
        OrderSDO orderSDO = new OrderSDO();
        orderSDO.setId(request.getId());

        orderSDO.setAreaCode(request.getAreaCode());
        orderSDO.setBisUnitCode(request.getBisUnitCode());
        orderSDO.setSaleChannelCode(request.getSaleChannel());
        orderSDO.setSaleChannelName(request.getSaleChannelName());

        //设置销售订单的Tag
        if (null != request.getOrderType() && Objects.nonNull(orderSDO)) {
            SetOrderTag.getOrderTag(request.getOrderType(),orderSDO);
        }
        orderSDO.setOrderType(request.getOrderType());
        orderSDO.setOrderTypeName(request.getOrderTypeName());
        orderSDO.setReceiveAddressCode(request.getDeliveryAddr());
        orderSDO.setReceiveDetailAddress(request.getDeliveryAddressDetail());
        orderSDO.setCustomerCode(request.getCustomerCode());
        orderSDO.setCustomerName(request.getCustomerName());
        orderSDO.setWareHouseCode(request.getShippingWarehouse());
        //2023.02.27 增加收货仓库编码 换货订单使用
        orderSDO.setReceiveWareHouseCode(request.getReceiveWareHouseCode());
        orderSDO.setOrganizationCode(request.getSalesOrganization());
        orderSDO.setChannelCode(request.getBuzChannel());
        orderSDO.setChannelName(request.getBuzChannelName());
        orderSDO.setSaleChannelCode(request.getSaleChannel());
        orderSDO.setSaleChannelName(request.getSaleChannelName());
        orderSDO.setDepartmentCode(request.getSaleDepartment());
        orderSDO.setDepartmentName(request.getSaleDepartmentName());
        orderSDO.setOrganizationName(request.getSalesOrganizationName());
        orderSDO.setCustomerAccounts(request.getCustomerAccounts());
        //PaymentInformationSDO paymentInformationSDO = new PaymentInformationSDO();
        /*if (StringUtils.isNotBlank(request.getCustomerAccounts())) {
            paymentInformationSDO.setOrderTotalFee(Long.parseLong(request.getCustomerAccounts()));
        }*/
        orderSDO.setCostCenter(request.getCostCenter());
        orderSDO.setCashCustomerName(request.getCashCustomerName());
        orderSDO.setCustomerReferenceNumber(request.getCustomerRefNo());
        orderSDO.setRemark(request.getRemark());
        orderSDO.setProductGroupCode(request.getGoodsGroup());
        orderSDO.setProductGroupName(request.getGoodsGroupName());
        //todo 单据日期
        if (StringUtils.isNotBlank(request.getUpdateTime())) {
            orderSDO.setGmtModified(DateFormatUtil.parseSimpleDateFormat(request.getUpdateTime()));
        }
        orderSDO.setCreateReason(request.getOrderReason());
        orderSDO.setCreatorId(request.getCreatorId());
        orderSDO.setCreatorName(request.getCreator());
        if (StringUtils.isNotBlank(request.getCustomerRefDate())) {
            SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = spf.parse(request.getCustomerRefDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            orderSDO.setCustomerExpectDate(date);
        }
        orderSDO.setContactor(request.getContactor());
        orderSDO.setContactorPhone(request.getPhone());
        orderSDO.setTransportModeCode(request.getDeliveryMethod());
        orderSDO.setSalesmanId(request.getSalesmanCode());
        return orderSDO;
    }

    public static OrderSDO respConverterSDO(OrderQueryResponse response) {
        OrderSDO orderSDO = new OrderSDO();
        orderSDO.setOrderCategoryCode(response.getOrderCategoryCode());
        orderSDO.setOrderBizId(response.getOrderBizId());
        orderSDO.setAreaCode(response.getAreaCode());
        orderSDO.setBisUnitCode(response.getBisUnitCode());
        orderSDO.setSalesmanId(response.getSalesmanCode());
        orderSDO.setSalesmanName(response.getSalesmanName());

        orderSDO.setOuterOrderId(response.getOuterId());
        orderSDO.setId(response.getId());
        orderSDO.setOrderTag(response.getOrderTag());
        if (StringUtils.isNotBlank(response.getOrderStatus())) {
            orderSDO.setStatus(Integer.valueOf(response.getOrderStatus()));
        }
        orderSDO.setReceiveAddressCode(response.getDeliveryAddr());
        orderSDO.setAddressCode(response.getAddressCode());
        orderSDO.setFrontOrderId(response.getFrontOrderId());
        if (StringUtils.isNotBlank(response.getFeatures())) {
            Map<String,String> orderFeatures = JSONObject.parseObject(response.getFeatures(), Map.class);
            orderSDO.setFeatures(orderFeatures);
        }
        orderSDO.setBizFeatures(response.getBizFeatures());
        orderSDO.setCustomerCode(response.getCustomerCode());
        orderSDO.setWareHouseCode(response.getShippingWarehouse());
        orderSDO.setWareHouseOutCode(response.getShippingWarehouseCode());
        //2023.02.27
        orderSDO.setReceiveWareHouseCode(response.getReceiveWareHouseCode());
        orderSDO.setReceiveWareHouseName(response.getReceiveWareHouseName());
        orderSDO.setOrganizationCode(response.getSalesOrganization());
        orderSDO.setChannelCode(response.getBuzChannel());
        orderSDO.setSaleChannelCode(response.getSaleChannel());
        orderSDO.setDepartmentCode(response.getSaleDepartment());
        orderSDO.setCostCenter(response.getCostCenter());
        orderSDO.setCashCustomerName(response.getCashCustomerName());
        orderSDO.setCustomerReferenceNumber(response.getCustomerRefNo());
        orderSDO.setRemark(response.getRemark());
        orderSDO.setProductGroupCode(response.getGoodsGroup());
        orderSDO.setProductGroupName(response.getGoodsGroupName());
        orderSDO.setCustomerAccounts(response.getCustomerAccounts());
        if (null != response.getUpdateTime()) {
            orderSDO.setGmtModified(DateFormatUtil.parseSimpleDateFormat(response.getUpdateTime()));
        }
        orderSDO.setCreateReason(response.getOrderReason());
        orderSDO.setCreatorName(response.getCreator());
        orderSDO.setCreatorId(response.getCreatorId());
        if (response.getCustomerRefDate() != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = null;
            try {
                parse = sdf.parse(response.getCustomerRefDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            orderSDO.setCustomerExpectDate(parse);
        }
        orderSDO.setContactor(response.getContact());
        orderSDO.setContactorPhone(response.getContactPhone());
        orderSDO.setTransportModeCode(response.getDeliveryMethod());
        orderSDO.setOrderType(response.getOrderType());
        orderSDO.setSyncVersion(response.getSyncVersion());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse = sdf2.parse(response.getOrderTime());
            orderSDO.setGmtCreate(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //子单
        List<OrderDetailDTO> orderDetailList = response.getOrderDetailList();
        if (CollectionUtils.isNotEmpty(orderDetailList)) {
            List<OrderLineSDO> orderLineSDOS = orderDetailList.stream().map(orderDetailDTO -> {
                OrderLineSDO orderLineSDO = new OrderLineSDO();

                orderLineSDO.setFrontOrderLineId(orderDetailDTO.getFrontOrderLineId());
                orderLineSDO.setOuterOrderLineId(orderDetailDTO.getOuterOrderLineId());
                orderLineSDO.setDeliveryAmount(orderDetailDTO.getDeliveryAmount());
                if (null!=orderDetailDTO.getOrderStatus()) {
                    orderLineSDO.setStatus(Integer.parseInt(orderDetailDTO.getOrderStatus()));
                }
                orderLineSDO.setId(orderDetailDTO.getId());
                orderLineSDO.setMainOrderId(response.getId());
                orderLineSDO.setOrderLineTag(orderDetailDTO.getOrderLineTag());
                orderLineSDO.setSyncVersion(orderDetailDTO.getSyncVersion());
                if (null != orderDetailDTO.getQuantity()) {
                    orderLineSDO.setQuantity(orderDetailDTO.getQuantity());
                }
                ScItemSDO scItemSDO = new ScItemSDO();
                scItemSDO.setScItemId(orderDetailDTO.getGoodsId());
                scItemSDO.setScItemTitle(orderDetailDTO.getGoodsName());
                if (null != orderDetailDTO.getQuantity()) {
                    scItemSDO.setQuantity(Integer.parseInt(orderDetailDTO.getQuantity()));
                }
                scItemSDO.setCategoryCode(orderDetailDTO.getCategoryCode());
                scItemSDO.setCategoryDesc(orderDetailDTO.getCategoryDesc());
                scItemSDO.setCategoryChildCode(orderDetailDTO.getCategoryChildCode());
                scItemSDO.setCategoryChildDesc(orderDetailDTO.getCategoryChildDesc());
                scItemSDO.setFirstCategoryCode(orderDetailDTO.getFirstCategoryCode());
                scItemSDO.setFirstCategoryDesc(orderDetailDTO.getFirstCategoryDesc());
                scItemSDO.setSecondCategoryCode(orderDetailDTO.getSecondCategoryCode());
                scItemSDO.setSecondCategoryDesc(orderDetailDTO.getSecondCategoryDesc());

                scItemSDO.setUnitPrice(StringUtils.isBlank(orderDetailDTO.getUnitPrice()) ? null : Long.valueOf(orderDetailDTO.getUnitPrice()));
                if (null != orderDetailDTO.getProducteDate()) {
                    scItemSDO.setProductationTime(DateFormatUtil.parseSimpleDateFormat(orderDetailDTO.getProducteDate()));
                }
                if (null != orderDetailDTO.getActivityUnitPrice()) {
                    scItemSDO.setActivityPrice(Long.valueOf((orderDetailDTO.getActivityUnitPrice())));
                }
                scItemSDO.setScItemOutCode(orderDetailDTO.getGoodsCode());

                scItemSDO.setSaleUnit(orderDetailDTO.getSaleUnit());
                scItemSDO.setSaleUnitName(orderDetailDTO.getSaleUnitName());
                scItemSDO.setInventoryUnitCode(orderDetailDTO.getInventoryUnitCode());
                scItemSDO.setInventoryUnitName(orderDetailDTO.getInventoryUnit());
                scItemSDO.setUnitConvert(orderDetailDTO.getUnitConvert());
                orderLineSDO.setScItemSDO(scItemSDO);
                orderLineSDO.setRemark(orderDetailDTO.getRemark());
                orderLineSDO.setSerialNumber(orderDetailDTO.getLineNum());

                orderLineSDO.setActivityCode(orderDetailDTO.getActivityId());
                orderLineSDO.setRefuseReasonCode(orderDetailDTO.getRejectReason());
                if (null != orderDetailDTO.getCumulativeShipmentQuantity()) {
                    orderLineSDO.setCumulativeReceiptQuantity(orderDetailDTO.getCumulativeShipmentQuantity().toString());
                }
                if (StringUtils.isNotBlank(orderDetailDTO.getFeatures())) {
                    Map<String,String> orderLineFeatures = JSONObject.parseObject(orderDetailDTO.getFeatures(), Map.class);
                    orderLineSDO.setFeatures(orderLineFeatures);
                }
                orderLineSDO.setSyncVersion(orderDetailDTO.getSyncVersion());
                return orderLineSDO;
            }).collect(Collectors.toList());
            orderSDO.setOrderLineSDOS(orderLineSDOS);
        }
        orderSDO.setDeliverOrderSDO(response.getDeliverOrderSDO());
        orderSDO.setFromSys(response.getFromSys());
        orderSDO.setBusinessType(response.getBusinessType());
        orderSDO.setFulfillmentPriority(response.getFulfillmentPriority());
        orderSDO.setFulfillmentTypeCode(response.getFulfillmentTypeCode());
        orderSDO.setAutoFulfillment(response.getAutoFulfillment());
        return orderSDO;
    }
}
